<template>
  <div class="app-container">
    <div class="filter-container">

      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit"
                 @click="handleCreate"
      >
        添加
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" min-width="30">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="80" align="center" label="服务名称">
        <template slot-scope="scope">
          <el-link  type="success">{{scope.row.name}}</el-link>
          <!--          <span>{{ scope.row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>-->
        </template>
      </el-table-column>


      <el-table-column min-width="80" align="center" label="服务金额">
        <template slot-scope="scope">
            <span>{{scope.row.fee}}</span>
          <!--          <span>{{ scope.row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>-->
        </template>
      </el-table-column>

      <el-table-column min-width="80" align="center" label="服务天数">
        <template slot-scope="scope">
          <span>{{scope.row.days}}</span>
          <!--          <span>{{ scope.row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>-->
        </template>
      </el-table-column>

      <el-table-column min-width="80" align="center" label="标签">
        <template slot-scope="scope">
          <span>{{scope.row.tag}}</span>
          <!--          <span>{{ scope.row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>-->
        </template>
      </el-table-column>


      <el-table-column min-width="80" align="center" label="描述">
        <template slot-scope="scope">
          <span>{{scope.row.desc}}</span>
          <!--          <span>{{ scope.row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>-->
        </template>
      </el-table-column>


      <el-table-column min-width="60" align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.created_at }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="100">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" circle @click="handleUpdate(scope.row)">
          </el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" circle @click="del(scope.row.id)">
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="getList"
    />
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="40%">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="120px"
               style="width: 90%; margin-left:5%;"
      >
        <el-form-item label="服务名称" prop="name">
          <el-input v-model="temp.name"  style="width: 80%"/>
        </el-form-item>
        <el-form-item label="服务金额" prop="fee">
          <el-input v-model="temp.fee" type="number" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="天数" prop="days">
          <el-input v-model="temp.days" type="number" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="标签" prop="tag">
          <el-input v-model="temp.tag" style="width: 80%"/>
        </el-form-item>
        <el-form-item style="margin-bottom: 20px;" label="描述">
          <el-input v-model="temp.desc" :rows="1" type="textarea" class="article-textarea" autosize
                    placeholder="请输入内容" style="width: 80%"
          />
     </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <center>
          <el-button @click="resetForm">
            取消
          </el-button>
          <el-button type="primary" @click="dialogStatus==='create'?createData():createData()">
            提交
          </el-button>
        </center>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible">
      <el-image width="100%" :src="dialogImageUrl" alt="" fit="contain"></el-image>
    </el-dialog>
  </div>
</template>

<script>
import {getList, create, del} from '@/api/service'
import Pagination from '@/components/Pagination'
import ImageCropper from '@/components/ImageCropper'
import PanThumb from '@/components/PanThumb'
export default {
  name: 'ChargeService',
  components: {Pagination, ImageCropper, PanThumb},
  computed: {
    // contentShortLength() {
    //   return this.temp.description.length
    // },
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      disabled: false,
      fileList:[],
      // chargeList:[],
      // chargeList1:[],
      imagecropperShow: false,
      imagecropperKey: 0,
      menus: [],
      roles: [],
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      selectAreas:'',
      selectParent:'',
      dialogImageUrl:'',
      dialogVisible:false,
      temp: {
        id: undefined,
        fee:'',
        tag:'',
        desc:'',
        days:'',
        name: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      filterText: '',
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      rules: {
        name: [{required: true, message: '服务名称不能为空', trigger: 'blur'}],
        fee: [{required: true, message: '金额不能为空', trigger: 'blur'}],
        tag: [{required: true, message: '标签不能为空', trigger: 'blur'}],
        days: [{required: true, message: '天数不能为空', trigger: 'blur'}]
      },
      addr: [],
      distributorsList: [],
      nowDistributor: '',
      disOptions: [],
      cityProps: {
        value: 'code',   // 指定选项的 值 为选项对象的某个属性值
        label: 'name',   // 指定选项 标签 为选项对象的某个属性值
        children: 'children'    //指定选项的 子选项 为选项对象的某个属性值
      },
      colors: [
        {color: '#f56c6c', percentage: 20},
        {color: '#e6a23c', percentage: 40},
        {color: '#5cb87a', percentage: 60},
        {color: '#1989fa', percentage: 80},
        {color: '#6f7ad3', percentage: 100}
      ],
      distributorsProps: {
        value: 'id',
        label: 'name',
        children: 'children',
        checkStrictly: true
      },
      selectCityProps: {
        value: 'code',   // 指定选项的 值 为选项对象的某个属性值
        label: 'name',   // 指定选项 标签 为选项对象的某个属性值
        children: 'children',    //指定选项的 子选项 为选项对象的某个属性值
        checkStrictly: true
      },
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getList()
  },
  methods: {

    handleRemove(file, fileList) {
      const key = this.fileList.indexOf(file)
      this.fileList.splice(key, 1)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleSuccess(file, fileList) {
      this.fileList.push(file.data)
    },
    changeStatus(val,id){
      changeStatus(val,id).then(response =>{
        this.$notify.success({
          title: '消息提示',
          message: response.msg,
          offset: 100
        })
        this.getList()
      })
    },
    getSelector(){
      all('all').then(response=>{
        this.distributorsList = response.data
      })
    },
    increase() {
      this.temp.rate += 5;
      if (this.temp.rate > 100) {
        this.temp.rate = 100;
      }
    },
    decrease() {
      this.temp.rate -= 5;
      if (this.temp.rate < 0) {
          this.temp.rate = 0;
      }
    },
    handleAddrChange(e) {
      // 获取当前选中节点的数组
      let addrNode = this.$refs['cascaderAddr'].getCheckedNodes()[0]
      let addrText = addrNode.pathLabels
      this.temp.province_name = addrText[0]
      this.temp.city_name = addrText[1]
      this.temp.area_name = addrText[2]
    },
    handleDistributorsChange(e){
      // this.editCharge()
    },
    cropSuccess(resData) {
      this.imagecropperShow = false
      this.imagecropperKey = this.imagecropperKey + 1
      this.temp.avatar = resData.url
    },
    close() {
      this.imagecropperShow = false
    },
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },

    getList() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    resetForm() {
      this.dialogFormVisible = false
      this.resetTemp()
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          create(this.temp).then(response => {
            this.dialogFormVisible = false
            if (response.code == 200) {
              this.$notify.success({
                title: '成功',
                message: response.msg,
                type: 'success',
                duration: 2000
              })
              this.resetTemp()
              this.getList()
            } else {
              this.$notify.error({
                title: '失败',
                message: response.msg,
                duration: 2000
              })
            }
          })
        }
      })
    },
    handleUpdate(row) {
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.temp = Object.assign({}, row) // copy obj
      // this.temp.areas = [row.province_id+'',row.city_id+'',row.area_id+'']
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    del(id) {
      this.$confirm('确认要删除该条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(id).then(response => {
          // this.list.unshift(this.temp)
          if (response.code == 200) {
            this.$notify.success({
              title: '成功',
              message: response.msg,
              type: 'success',
              duration: 2000
            })
            this.resetTemp()
            this.getList()
          } else {
            this.$notify.error({
              title: '失败',
              message: response.msg,
              duration: 2000
            })
          }
        })
      }).catch(() => {

      })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        fee:'',
        tag:'',
        desc:'',
        days:'',
        name: ''
      }
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}

.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
<style scoped>
.avatar {
  width: 200px;
  height: 200px;
  border-radius: 50%;
}
</style>
<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
</style>
